package com.itboye.sunsun;

import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.controller.UnLoginState;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.umeng.message.PushAgent;

public class MainActivity extends BaseActivity implements OnClickListener {

	private Fragment[] fragments=new Fragment[3];
//	private HomeFragment homeFragment;
//	private NoticeFragment noticeFragment;
//	private MeFragment meFragment;
	private ImageView[] tabs;
	private TextView[] texts;
	private int index;
	private int currentIndex;
	
	private View tabFirstPage;
	private View tabAnnounce;
	private View tabMe;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 
		isGestureSensitive(false);
	//	new PhotoSelectFragment().show(getSupportFragmentManager(), "");
//		startActivity(new Intent(getApplicationContext(), StartActivity.class));
		initView();

		
		List<Fragment>list=getSupportFragmentManager().getFragments();
		
		if (list!=null) {
			for (int i = 0; i < fragments.length; i++) {
				fragments[i]=getSupportFragmentManager().findFragmentByTag(""+i);
			}
			index=savedInstanceState.getInt("tab");
			switch (index) {
			case 0:
				onClick(tabFirstPage);
				break;

			case 1:
				onClick(tabAnnounce);	
				break;
			case 2:
				onClick(tabMe);
				break;
			}
		}else{
			fragments[0]=new HomeFragment();
			
//			fragments[0]=new ForumHoneFragment();
			getSupportFragmentManager().beginTransaction()
			.add(R.id.fragment_container, fragments[0],"0").show(fragments[0])
			.commit();
		}
		
//		homeFragment = new HomeFragment(MainActivity.this);
//		noticeFragment = new NoticeFragment(MainActivity.this);
//		meFragment = new MeFragment(MainActivity.this);
//		fragments = new Fragment[] { homeFragment, noticeFragment, meFragment };
//
//		getSupportFragmentManager().beginTransaction()
//				.add(R.id.fragment_container, homeFragment).show(homeFragment)
//				.commit();

		try {
			PushAgent.getInstance(getApplicationContext()).enable();
		} catch (Exception e) {
			 
		}
		if ((Boolean) SPUtils.get(App.ctx, null, SPContants.IS_LOGINED, false)) {
			LoginController.setLoginState(new LoginedState());
		}else{
			LoginController.setLoginState(new UnLoginState());
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		try {
			PushAgent.getInstance(getApplicationContext()).enable();
		} catch (Exception e) {
			 
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		tabs = new ImageView[3];
		tabs[0] = (ImageView) findViewById(R.id.btn_infor);
		tabs[1] = (ImageView) findViewById(R.id.btn_device);
		tabs[2] = (ImageView) findViewById(R.id.btn_me);
		tabs[0].setSelected(true);//
		texts = new TextView[3];
		texts[0] = (TextView) findViewById(R.id.txt_infor);
		texts[1] = (TextView) findViewById(R.id.txt_device);
		texts[2] = (TextView) findViewById(R.id.txt_me);
		texts[0].setSelected(true);//
		
		tabAnnounce.setOnClickListener(this);
		tabFirstPage.setOnClickListener(this);
		tabMe.setOnClickListener(this);

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		outState.putInt("tab", index);
	}
	
	 
	@SuppressLint("NewApi") public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.tabFirstPage) {
			index = 0;
			if (fragments[0]==null) {
				fragments[0]= new HomeFragment(); 
			}
			tabs[0].setBackgroundDrawable(getResources().getDrawable(
					R.drawable.homepage_on));
			tabs[1].setBackgroundDrawable(getResources().getDrawable(R.drawable.notice_off));
			tabs[2].setBackgroundDrawable(getResources().getDrawable(R.drawable.me_off));

			texts[0].setTextColor(getResources().getColor(R.color.home_blue));
			texts[1].setTextColor(getResources().getColor(R.color.home_gray));
			texts[2].setTextColor(getResources().getColor(R.color.home_gray));
		}
		if (id == R.id.tabAnnounce) {
			index = 1;
			if (fragments[1]==null) {
				fragments[1]= new NoticeFragment(MainActivity.this); 
			}
			tabs[1].setBackgroundDrawable(getResources().getDrawable(
					R.drawable.notice_on));
			tabs[2].setBackgroundDrawable(getResources().getDrawable(R.drawable.me_off));
			tabs[0].setBackgroundDrawable(getResources().getDrawable(R.drawable.homepage_off));

			texts[0].setTextColor(getResources().getColor(R.color.home_gray));
			texts[1].setTextColor(getResources().getColor(R.color.home_blue));
			texts[2].setTextColor(getResources().getColor(R.color.home_gray));
		}
		if (id == R.id.tabMe) {
			index = 2;
			if (fragments[2]==null) {
				fragments[2]= new MeFragment(MainActivity.this); 
			}
			tabs[0].setBackgroundDrawable(getResources().getDrawable(R.drawable.homepage_off));
			tabs[1].setBackgroundDrawable(getResources().getDrawable(R.drawable.notice_off));
			tabs[2].setBackgroundDrawable(getResources().getDrawable(R.drawable.me_on));

			texts[0].setTextColor(getResources().getColor(R.color.home_gray));
			texts[1].setTextColor(getResources().getColor(R.color.home_gray));
			texts[2].setTextColor(getResources().getColor(R.color.home_blue));
		}
//		if (index != currentIndex) {
//			android.support.v4.app.FragmentTransaction trx = getSupportFragmentManager()
//					.beginTransaction();
//			trx.hide(fragments[currentIndex]);
//			if (!fragments[index].isAdded()) {
//				trx.add(R.id.fragment_container, fragments[index]);
//			}
//			trx.show(fragments[index]).commit();
//			tabs[currentIndex].setSelected(false);
//			tabs[index].setSelected(true);
//			currentIndex = index;
//		}
//		
		
		for (int j = 0; j < fragments.length; j++) {
			if (fragments[j]!=null) {
				getSupportFragmentManager().beginTransaction().hide(fragments[j]).commitAllowingStateLoss();
			}
		}
		
		if (!fragments[index].isAdded()) {
			getSupportFragmentManager().beginTransaction()
			.add(R.id.fragment_container, fragments[index],""+index).commitAllowingStateLoss();
		}
		
		getSupportFragmentManager().beginTransaction().show(fragments[index]).commitAllowingStateLoss();
		
		
		if (id==R.id.tabMe) {
			setStatusBarColor(R.color.top_blue_different);
		}else{
			setStatusBarColor(R.color.orange);
		}
	}
	
 


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 
		if (keyCode==KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
			return true;
		}else
			return super.onKeyDown(keyCode, event);
	}
	
	
	 
}
